package com.nyist.myticket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nyist.myticket.biz.entity.JobTask;
import com.nyist.myticket.biz.mapper.TbJobTaskMapper;
import com.nyist.myticket.biz.service.TbJobTaskService;
import com.nyist.myticket.core.base.BaseService;
import com.nyist.myticket.core.enums.DataStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangxiaogen
 * @since 2021-01-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbJobTaskServiceImpl extends BaseService<TbJobTaskMapper, JobTask> implements TbJobTaskService {

    @Autowired
    private TbJobTaskMapper taskMapper;

    @Override
    public JobTask saveJob(JobTask jobTask) {
        int count = taskMapper.insert(jobTask);
        if (count > 0){
            return jobTask;
        }
        return new JobTask();
    }

    @Override
    public boolean deleteToChangeStatus(JobTask jobTask) {
        jobTask.setStatus(DataStatusEnum.REMOVE.getCode());
        int count = taskMapper.updateById(jobTask);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public JobTask updateJob(JobTask jobTask) {
        int count = taskMapper.updateById(jobTask);
        if (count > 0) {
            return jobTask;
        }
        return new JobTask();
    }

    @Override
    public JobTask queryJobById(JobTask jobTask) {
        QueryWrapper<JobTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", jobTask.getId());
        queryWrapper.eq("status", jobTask.getStatus());
        return taskMapper.selectOne(queryWrapper);
    }

    @Override
    public List<JobTask> queryJobAll(String keywords) {
        QueryWrapper<JobTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keywords);
        return taskMapper.selectList(queryWrapper);
    }
}
